package csu.rjxyg.util.impl;

import csu.rjxyg.action.constant.InfoConstant;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.util.SortUtil;

public class SortUtilImpl implements SortUtil {

	@Override
	public BaseList sort(BaseList baseList, String fieldName, int status) throws Exception{
		BaseObject temp1;
		BaseObject temp2;
		
		for( int i = 0; i < baseList.size();i++ ){
			
			for( int j = 0; j + 1 < baseList.size() - i;j++ ){
				temp1 = baseList.get(j);
				temp2 = baseList.get(j + 1);
				 Object temp1Object = temp1.get(fieldName);
				 Object temp2Object = temp2.get(fieldName);
				 if( temp1Object.getClass().getName().equals("java.lang.String") ){
					 if( status == 1){
						 if( ( (String) temp1Object ).compareTo((String) temp2Object) < 0){
							 baseList.remove(j);
							 baseList.remove(j);
							 baseList.add(j, temp2);
							 baseList.add(j+1, temp1);
						 }
					 }else if( status == -1){
						 if( ( (String) temp1Object ).compareTo((String) temp2Object) > 0){
							 baseList.remove(j);
							 baseList.remove(j);
							 baseList.add(j, temp2);
							 baseList.add(j+1, temp1);
						 }
					 }
					
				 }else{
					 System.out.println("暂不支持其他类型排序");
				 }
			}
		}
		return baseList;
	}

}
